package com.changzhi.controller;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.changzhi.common.annotation.Verify;
import com.changzhi.common.util.Result;
import com.changzhi.common.util.SignUtils;
import com.changzhi.ldyl.model.dto.NewYearDto;
import com.changzhi.service.NewYearService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:元旦活动
 * @author: 小落
 * @date: 2020年11月02日
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("newYear")
public class NewYearController {

	@Autowired
	private NewYearService newYearService;

	/**
	 * 卡牌列表
	 * @return
	 */
	@Verify
	@PostMapping("cardList")
	public Result cardList(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		Object data = newYearService.cardList(newYearDto);
		log.info("methodName:cardList,耗时={}", System.currentTimeMillis() - start);
		return Result.success(data);
	}

	/**
	 * 翻牌
	 * @return
	 */
	@Verify
	@PostMapping("openCard")
	public Result openCard(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		Object data = newYearService.openCard(newYearDto);
		log.info("methodName:openCard,耗时={}", System.currentTimeMillis() - start);
		return Result.success(data);
	}

	/**
	 * 一键全翻
	 * @return
	 */
	@Verify
	@PostMapping("openCardByLevel")
	public Result openCardByLevel(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		Object data = newYearService.openCardByLevel(newYearDto);
		log.info("methodName:openCardByLevel,耗时={}", System.currentTimeMillis() - start);
		return Result.success(data);
	}

	/**
	 * 奖品兑换列表
	 * @return
	 */
	@Verify(login = false)
	@PostMapping("prizeList")
	public Result prizeList(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		Object data = newYearService.prizeList(newYearDto);
		log.info("methodName:prizeList,耗时={}", System.currentTimeMillis() - start);
		return Result.success(data);
	}

	/**
	 * 兑换奖品
	 * @return
	 */
	@Verify
	@PostMapping("exchangePrize")
	public Result exchangePrize(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		Object data = newYearService.exchangePrize(newYearDto);
		log.info("methodName:exchangePrize,耗时={}", System.currentTimeMillis() - start);
		return Result.success(data);
	}

	/**
	 * 兑换火苗
	 * @return
	 */
	@Verify
	@PostMapping("exchangeFlame")
	public Result exchangeFlame(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		newYearService.exchangeFlame(newYearDto);
		log.info("methodName:exchangeFlame,耗时={}", System.currentTimeMillis() - start);
		return Result.success();
	}

	/**
	 * 领取火苗
	 * @return
	 */
	@Verify
	@PostMapping("receiveFlame")
	public Result receiveFlame(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		newYearService.receiveFlame(newYearDto.getUserId());
		log.info("methodName:receiveFlame,耗时={}", System.currentTimeMillis() - start);
		return Result.success();
	}

	/**
	 * 余额
	 * @return
	 */
	@Verify
	@PostMapping("balance")
	public Result balance(@RequestBody @Validated NewYearDto newYearDto){
		long start = System.currentTimeMillis();
		Map<String, Object> balance = newYearService.balance(newYearDto.getUserId());
		log.info("methodName:balance,耗时={}", System.currentTimeMillis() - start);
		return Result.success(balance);
	}

	/**
	 *
	 * @return
	 */
	/*@PostMapping("test")
	public Result test(){
		for (int i = 0; i < 100; i++) {
			newYearService.lottery("1295306873754353664");
		}
		return Result.success();
	}*/
	public static void main(String[] args) {
		//请求api
		Map<String, Object> params = new HashMap<>();
		params.put("channelId", "1264830538691117057");
		params.put("type", 0);
		params.put("pwd", "8704");
		params.put("roundIndex", 2);
		String requestBody = JSON.toJSONString(params);
		log.warn("methodName:clearChannelReBoot,requestBody={}",requestBody);
		HttpRequest request = HttpRequest.post("https://test.xbyy.live/" + "/rest/activity/addChannelRobotByFirst").header("sign", SignUtils
				.sign(params)).body(requestBody);
		String response = request.execute().body();
		log.warn("methodName:clearChannelReBoot,response={}",response);
		Result result = JSON.parseObject(response, Result.class);
		if(result.getCode() != 0){
			log.error("methodName:clearChannelReBoot,response={}",response);
		}
	}
}
